<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=utf-8" errorPage="/error.jsp" isELIgnored="false" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache" %>
<%@ taglib uri="http://atleap.blandware.com/taglib" prefix="atleap" %>
<html:xhtml />

<atleap:constants className="com.blandware.atleap.common.Constants" var="USER_KEY" scope="page" />
<atleap:constants className="com.blandware.atleap.webapp.listener.UserCounterListener" var="COUNT_KEY" />
<atleap:constants className="org.acegisecurity.ui.AbstractProcessingFilter" var="ACEGI_SAVED_REQUEST_KEY" />
<atleap:constants className="com.blandware.atleap.webapp.util.core.WebappConstants" var="AUTH_TARGET_URL_KEY" />

<div id="appEntrance">
    <c:choose>
        <c:when test="${pageContext.request.remoteUser == null}">
        <%-- User is not logged in, display login form --%>

            <%-- If you don't want to encrypt passwords programmatically, or you don't
                care about using SSL for the login, you can change this form's action
                to "j_security_check" --%>
            <form method="POST" id="loginForm" action="<atleap:rewriteUrl href="/servlet/authorize" />"
                onsubmit="saveUsername(this);var isValid = validateLoginForm(this); disableButtons(this, isValid); return isValid;" name="loginForm">
                <table width="100%" border="0">
                    <tr>
                        <td colspan="2">
                            <c:if test="${param.error != null}">
                                <div class="error">
                                    <html:img pageKey="core.commons.icon.warning.img" altKey="core.commons.icon.warning" styleClass="icon"/>
                                    <atleap:message key="core.commons.errors.password.mismatch" />
                                </div>
                            </c:if>
                         </td>
                    </tr>
                    <tr>
                        <th align="left" nowrap="nowrap">
                            <label for="j_username" class="required">
                                *&nbsp;<atleap:message key="core.commons.label.username"/>:
                            </label>
                        </th>
                        <td align="right">
                            <input class="text" type="text" name="j_username" id="j_username" tabindex="1" />
                        </td>
                    </tr>
                    <tr>
                        <th align="left" nowrap="nowrap">
                            <label for="j_password" class="required">
                                *&nbsp;<atleap:message key="core.commons.label.password"/>:
                            </label>
                        </th>
                        <td align="right">
                            <input class="text" type="password" name="j_password" id="j_password" tabindex="2" />
                        </td>
                    </tr>
                    <c:if test="${rememberMeEnabled}">
                        <tr>
                            <td colspan="2" align="left">
                                <input type="checkbox" class="rememberCheckbox" name="rememberMe" id="rememberMe" tabindex="3" />
                                <label for="rememberMe"><atleap:message key="core.commons.pages.login.rememberMe"/></label>
                            </td>
                        </tr>
                    </c:if>
                    <tr>
                        <td align="left">
                            <!-- for Resin (and to protect from session invalidation
                                while entering login/password) -->
                            <c:set var="uri" value=""/>
                            <c:choose>
                                <c:when test="${!empty sessionScope[pageScope.AUTH_TARGET_URL_KEY]}">
                                    <c:set var="uri" value="${sessionScope[pageScope.AUTH_TARGET_URL_KEY]}"/>
                                </c:when>
                                <c:otherwise>
                                    <c:set var="savedRequest" value="${sessionScope[pageScope.ACEGI_SAVED_REQUEST_KEY]}"/>
                                    <c:if test="${savedRequest != null}">
                                        <c:set var="uri" value="${savedRequest.requestURL}"/>
                                    </c:if>
                                </c:otherwise>
                            </c:choose>
                            <input type="hidden" name="j_uri" id="j_uri" value="${uri}" />
                            <input type="submit" class="button" name="login" id="login" value="<atleap:message key="core.commons.buttons.login"/>" tabindex="4" />
                        </td>
                        <td align="right">
                            <input type="reset" class="button" name="reset" id="reset" value="<atleap:message key="core.commons.buttons.reset"/>" tabindex="5"
                                   onclick="document.getElementById('j_username').focus()" style="width: 80px;" />
                        </td>
                    </tr>
                </table>
            </form>
            <script type="text/javascript">
                <!--

                    function saveUsername(theForm) {
                        var expires = new Date();
                        expires.setTime(expires.getTime() + 24 * 30 * 60 * 60 * 1000); // sets it for approx 30 days.
                        setCookie("username",theForm.j_username.value,expires,"<atleap:rewriteUrl href="/" />");
                    }

                    function validateLoginForm(form) {
                        return validateRequired(form);
                    }

                    function loginForm_required () {
                        this.aa = new Array("j_username", "<atleap:message key="core.commons.label.username" var="usernameLabel"/><atleap:message key="core.commons.errors.required" arg0="${usernameLabel}" />", new Function ("varName", " return this[varName];"));
                        this.ab = new Array("j_password", "<atleap:message key="core.commons.label.password" var="passwordLabel"/><atleap:message key="core.commons.errors.required" arg0="${passwordLabel}" />", new Function ("varName", " return this[varName];"));
                    }

                // -->
            </script>           
        </c:when>
        <c:otherwise>
            <%-- User is logged in --%>
            <atleap:rewriteUrl href="/logout.jsp" var="logoutUrl" addLocaleSuffix="false" />
            <form method="POST" action="${logoutUrl}" id="logoutForm" name="logoutForm" onsubmit="disableButtons(this, true);">
                <atleap:isUserInRole roles="core-commons-viewActiveUsers">
                    <div align="center" class="activeUsers">
                        <atleap:message key="core.commons.pages.login.activeUsers" arg0="${requestScope[pageScope.COUNT_KEY]}" />
                    </div>
                </atleap:isUserInRole>
                <div align="center" class="welcomeMessage">
                    <atleap:message key="core.commons.pages.login.welcomeMessage" arg0="${sessionScope[pageScope.USER_KEY].fullName}" />
                </div>
                <div align="center" class="logoutButton">
                    <input type="submit" value="<atleap:message key="core.commons.buttons.logout" />" class="button" name="logout" id="logout" />
                </div>
            </form>
        </c:otherwise>
    </c:choose>
</div>
